<?php
/**
 * CalendarModel handles the database interactions for calendar/event actions
 *  
 * @author Jimmy E. Lewis
 * Updated on: May 14, 2008
 */
class CalendarModel extends Model {
	
	function __construct() {
		parent::__construct();
		$this->load->helper('date');		
	}
	
/** ~*~*~*~*~*~*~*~*~*~			Event Functions 		~*~*~*~*~*~*~*~*~*~			*/
	
	/**
	 * Creates a calendar event with post data.
	 * @param $post An array containing an event to add to the event table
	 */
	function createEvent($post) {
		$this->db->insert('cms_calendar_events',$post);
	}
	
	/**
	 * Gets the event that an item belongs to.
	 * @param $id item's id
	 * @return an event belonging to the item
	 */
	function getEventForItem($id){
		$this->db->select('event_id')->from('cms_calendar_items')->where('id',$id);
		$event = $this->db->get()->row();
		$this->db->select('*')->from('cms_calendar_events')->where('id',$event->event_id);
		return $this->db->get()->row();
	}
	
	/**
	 * Updates an event.
	 * @param $id An event's id.
	 * @param $post An array containing the updated event
	 */
	function updateEvent($id, $post) {
		$this->db->where('id', $id);
		$this->db->update('cms_calendar_events', $post);
	}
	
	/**
	 * Updates repeatings events from the $startDay with repeating_id == $repeatingID
	 * @param $startDay A start day to start updating events on
	 * @param $repeatingID An event's repeatingID
	 */
	function updateRepeatingFrom($startDay,$repeatingID,$post){
		$this->db->select('id')->from('cms_calendar_events')->where('repeating_id',$repeatingID)->where('start >=',$startDay);
		
		$events = $this->db->get();
		
		if ($events->num_rows() > 0){
			foreach ($events->result() as $row){
				$this->updateEvent($row->id,$post);
			}
		}
		echo 'hey';
	}
	
	/**
	 * Deletes an event and any items that belongs to it.
	 * @param $id An event's id.
	 */
	function deleteEvent($id){
		$this->db->where('id',$id);
		$this->db->delete('cms_calendar_events');
		
		//also delete all items for this event;
		$items = $this->getItems($id);
		if ($items->num_rows() > 0) {
			foreach ($items->result() as $row) {
				$this->deleteItem($row->id);
			}
		}
		
		//also delete all rsvps
		$rsvp = $this->getRSVPs($id);
		if ($rsvp->num_rows() > 0) {
			foreach ($rsvp->result() as $row) {
				$this->deleteRSVP($row->id);
			}
		}
	}
	
	/**
	 * Deletes a repeating event and any items that belongs to them.
	 * @param $repeatingID An event's repeatingID
	 */
	function deleteRepeatingEvent($repeatingID){
		$this->db->select('id')->from('cms_calendar_events')->where('repeating_id',$repeatingID);
		$events = $this->db->get();
		
		if ($events->num_rows() > 0){
			foreach ($events->result() as $row){
				$this->deleteEvent($row->id);
			}
		}	
	}
	
	/**
	 * Deletes repeatings events from the $startDay with repeating_id == $repeatingID
	 * @param $startDay A start day to start deleting events on
	 * @param $repeatingID An event's repeatingID
	 */
	function deleteRepeatingFrom($startDay,$repeatingID){
		$this->db->select('id')->from('cms_calendar_events')->where('repeating_id',$repeatingID)->where('start >=',$startDay);
		
		$events = $this->db->get();
		
		if ($events->num_rows() > 0){
			foreach ($events->result() as $row){
				$this->deleteEvent($row->id);
			}
		}
	}
	/**
	 * Gets events pertaining to a user's groups during a time period
	 * @param $start A beginning unix timestamp
	 * @param $end An ending unix timestamp
	 * @param $userGroup A mysql array containing a users groups OR a string of 'xa' if the user isn't logged in
	 */
	function getEvents($start,$end,$userGroup){
		$start = date('Y-m-d H:i:s',$start);
		$end = date('Y-m-d H:i:s',$end);
		
		//always get XA events
		$where = "((start >='$start' AND end <='$end') OR (start <= '$end' AND end >= '$start')) && (category='XA'";
		
		if($userGroup!='xa'){
			foreach($userGroup->result() as $group){
				$where .= " || category='$group->groupid'";
			}
		}
		$where .= ')';
		$this->db->select('*')->from('cms_calendar_events')->where($where)->orderby('start','asc');
//		echo "<br>" . $this->db->last_query() . "<br>";
		return $this->db->get();
	}
	
	/**
	 * Gets events pertaining to a user's groups during a time period
	 * @param $start A beginning unix timestamp
	 * @param $end An ending unix timestamp
	 * @param $userGroup A mysql array containing a users groups OR a string of 'xa' if the user isn't logged in
	 */
	function getAllEvents($userGroup){
		//always get XA events
		$where = "`category` = 'XA'";
		
		//get user's group categories also
		if($userGroup!='xa'){
			foreach($userGroup->result() as $group){
				$where .= " || `category` = '$group->groupid'";
			}
		}
		$where .= '';
		$this->db->select('*')->from('cms_calendar_events')->where($where)->orderby('start','asc');
		return $this->db->get();
	}
	/**
	 * Gets event with the given id number.
	 * @param $id An events id.
	 */
	function getEvent($id){
		$this->db->select('*')->from('cms_calendar_events')->where('id',$id);
		return $this->db->get()->row();
		
	}
	
	/**
	 * Gets rsvps for an event.
	 * @param $info An array containing 1. user id 2. event id
	 * @param $type The type of rsvp to get (yes, no, or maybe) OR getUserStatus to get the users rvsp status for an event
	 * @return if: MySQL row with a users rsvp status for an event
	 * @return else: A mysql array with rsvp statuses for an event
	 */
	function getRSVP($info,$type='getUserStatus'){
		if($type=='getUserStatus'){
			$this->db->select('answer')->from('cms_calendar_rsvp')->where('user_id',$info['userid'])->where('event_id',$info['eventid']);
			return $this->db->get()->row();
		}
		else{
			$this->db->select('*')->from('cms_calendar_rsvp')->join('users','cms_calendar_rsvp.user_id = users.userid')->where('event_id',$info['eventid'])->where('answer',$type)->orderby('firstname','asc');
			return $this->db->get();
		}
	}
	
	/**
	 * Get RSVPs for an event
	 * @param $event_id An events id
	 */
	function getRSVPs($event_id){
		$this->db->select('*')->from('cms_calendar_rsvp')->where('event_id',$event_id);
		return $this->db->get();
	}
	/**
	 * Creates a rsvp to an event
	 * @param $post An array containing an rsvp to add to the rsvp table
	 */
	function createRSVP($post){
		$this->db->insert('cms_calendar_rsvp',$post);
	}
	
	/**
	 * Updates an user rsvp status
	 * @param $info An array containing 1. user_id 2. event_id
	 * @param $post An array containing an rsvp to update in the rsvp table
	 */
	function updateRSVP($info,$post){
		$this->db->where('user_id', $info['user_id'])->where('event_id',$info['event_id']);
		$this->db->update('cms_calendar_rsvp', $post);
	}
	
	/**
	 * Deletes an user rsvp status
	 * @param $id RSVP id
	 */
	function deleteRSVP($id){
		$this->db->where('id',$id);
		$this->db->delete('cms_calendar_rsvp');
	}
	
/** ~*~*~*~*~*~*~*~*~*~			Item Functions 		~*~*~*~*~*~*~*~*~*~			*/

	/**
	 * Creates an item for an event.
	 * @param $post Array containing an item to add to the item table
	 */
	function createitem($post) {
		$this->db->insert('cms_calendar_items',$post);
	}
	
	/**
	 * Gets an item from the item table.
	 * @param $id An items id.
	 * @return an item from the item table
	 */
	function getItem($id){
		$this->db->select('*')->from('cms_calendar_items')->where('id',$id);
		return $this->db->get()->row();
		
	}
	
	/**
	 * Gets all items for an event.
	 * @param $id An events id.
	 * @return all items for an event
	 */
	function getItems($id){
		$this->db->select('*')->from('cms_calendar_items')->where('event_id',$id)->orderby('name','asc');
		return $this->db->get();
	}
	
	/**
	 * Deletes an item
	 * @param $id An items id.
	 */
	function deleteItem($id){
		$this->db->where('id',$id);
		$this->db->delete('cms_calendar_items');
	}
	
	/**
	 * Assigns an item to a user.
	 * @param $id An items id.
	 * @param $post Array containing an item to add to the item table
	 */
	function assignItem($id,$post){
		$this->db->where('id', $id);
		$this->db->update('cms_calendar_items',$post);
	}
	
	/**
	 * Does an item already exist for an event
	 * @param $post['name'] An item
	 * @param $post['event_id'] An event's id.
	 */
	function itemDoesNotExist($post){
		$this->db->select('*')->from('cms_calendar_items')->where('event_id',$post['event_id'])->where('name',$post['name']);
		$items = $this->db->get();
		
		if ($items->num_rows()==0) return true;
		else return false;
	}
	
	/**
	 * Gets events on homepage.
	 * @param $limit Number of events to display
	 * @return MySQL array with no more then the limit of events
	 */
	function homepage($limit){
		$start = date("Y-n-j G:i:s",strtotime("today 1:00 am"));
		$this->db->select('*')->from('cms_calendar_events')->where('homepage','1')->where('start >=',$start)->orderby('start','asc')->limit($limit);
		return $this->db->get();
	}
	
	/**
	 * Gets events from today to the specified time.
	 * @param $limit A string time ex "1 day","1 week","1 month"
	 * @return MySQL array with events from today plus the $limit
	 */
	function getEventsTo($limit){
		$this->db->select('*')->from('cms_calendar_events')->where('start >=',date("Y-n-j G:i:s",strtotime("today 1:00 am")))->where('end <= ',date("Y-n-j G:i:s",strtotime("today + $limit")))->orderby('start','asc');
		return $this->db->get();
	}
	
	/**
	 * Gets upcoming events including today.
	 * @return MySQL array with all upcoming events
	 */
	function getUpcomingEvents(){
		$this->db->select('*')->from('cms_calendar_events')->where('start >=',date("Y-n-j"))->orderby('start','asc');
		return $this->db->get();
	}
	
	/**
	 * Gets previous events not including today.
	 * @return MySQL array with all upcoming events
	 */
	function getPastEvents(){
		$this->db->select('*')->from('cms_calendar_events')->where('start <',date("Y-n-j"))->orderby('start','asc');
		return $this->db->get();
	}
	
	/**
	 * 
	 */
	function getRepeatingEventsFrom($start,$repeatingID){
		$this->db->select('*')->from('cms_calendar_events')->where('repeating_id',$repeatingID)->where('start >=',$start);
		return $this->db->get();
	}
	
	/**
	 * 
	 */
	function getRepeatingEventsCustom($query){
		$query = $this->db->query($query);
		return $query;
	}
}